package com.ljl.forum_1.controller;

import com.ljl.forum_1.common.AppResult;
import com.ljl.forum_1.common.ResultCode;
import com.ljl.forum_1.config.AppConfig;
import com.ljl.forum_1.model.Article;
import com.ljl.forum_1.model.Board;
import com.ljl.forum_1.model.User;
import com.ljl.forum_1.services.IArticleService;
import com.ljl.forum_1.services.IBoradService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Keafmd
 *
 * @ClassName: ArticleController
 * @Description: ArticleController
 * @author: lijl
 * @date: 2025/2/9 10:54
 * @Blog: https://keafmd.blog.csdn.net/
 */
@Api(tags = "文章接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;
    @Resource
    private IBoradService boradService;

    // 发布新帖
    @ApiOperation("发布新帖")
    @PostMapping("/create")
    public AppResult create (HttpServletRequest request,
                             @ApiParam("板块ID") @RequestParam("boardId") @NonNull Long boardId,
                             @ApiParam("标题") @RequestParam("title") @NonNull String title,
                             @ApiParam("内容") @RequestParam("content") @NonNull String content) {
        // 校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1) {
            // 该用户被禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 板块的校验
        Board board = boradService.selectById(boardId.longValue());
        if (board == null || board.getDeleteState() == 1 || board.getState() == 1) {
            // 板块状态异常
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }

        // 封装文章对象
        Article article = new Article();
        article.setBoardId(boardId);    // 板块id
        article.setTitle(title);        // 标题
        article.setContent(content);    // 内容
        article.setUserId(user.getId());// 作者id

        // 调用service
        articleService.create(article);
        // 发帖成功
        return AppResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId (@ApiParam("板块Id") @RequestParam(value = "boardId", required = false) Long boardId) {

        List<Article> articles;

        if(boardId == null) {
            // 查询所有
            articles = articleService.selectAll();
        }else {
            // 查询对应版块的帖子
            articles = articleService.selectAllByBoardId(boardId);
        }
        if(articles == null) {
            // 如果结果集为空，那么创建上个空集合
            articles = new ArrayList<>();
        }

        // 响应结果
        return AppResult.success(articles);
    }

    @ApiOperation("根据帖子id查询帖子详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request,
                                         @ApiParam("帖子id") @RequestParam("id") @NonNull Long id) {
        // 从session中获取当前登录的用户
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);

        // 调用service,获取帖子详情
        Article article = articleService.selectDetailById(id);
        // 判断结果是否为空
        if (article == null) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 校验是否是当前登录用户发的帖子
        if (user.getId() == article.getUserId()) {
            article.setOwn(true);
        }
        // 返回结果
        return AppResult.success(article);
    }

    @ApiOperation("编辑帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id,
                            @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("帖子内容") @RequestParam("content") @NonNull String content) {
        // 获取当前登录的用户
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        // 校验用户状态
        if (user.getState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 查询帖子详情
        Article article = articleService.selectById(id);
        if (article == null) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        // 判断用户是不是作者
        if (user.getId() != article.getUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 判断帖子的状态
        if (article.getState() == 1 || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        // 调用service方法
        articleService.modify(id, title, content);
        // 打印日志
        log.info("更新帖子成功, article id = "+ id + ", user id = " + user.getId() + ".");
        // 返回正确响应
        return AppResult.success();
    }

    @ApiOperation("点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp (HttpServletRequest request,
                               @ApiParam("帖子id") @RequestParam("id") @NonNull Long id) {
        // 校验用户的状态
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        // 判断用户是否被禁言
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 调用Service
        articleService.thumbsUpById(id);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("帖子id") @RequestParam("id") @NonNull Long id) {
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            // 表示用户禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 查询帖子详情
        Article article = articleService.selectById(id);
        if (article == null ||article.getDeleteState() == 1) {
            // 帖子已删除
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 校验当前用户是否是该帖子的作者
        if (user.getId() != article.getUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 调用service
        articleService.deleteById(id);
        // 返回操作成功
        return AppResult.success();
    }
}
